package com.yylending.las.base;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import com.yyfax.common.CLog;
import com.yylending.las.Global;
import com.yylending.las.YJKD;
import com.yylending.las.authority.fingerprint.FingerprintValidateActivity;
import com.yylending.las.authority.gestures.GesturesPresenter;
import com.yylending.las.authority.gestures.ValidateGesturesActivity;

/**
 * Created by HenryZhang on 2016/11/21.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 *
 * http://www.jianshu.com/p/6b3b1449123b
 *
 * 判断手势密码，甚至是自动弹出登录界面
 */
public class ActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
	private final static String TAG = "ActivityLifecycleCallbacks";

	private int count;

	@Override
	public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
		CLog.i(TAG, "Activity onCreated: " + activity.getClass().getSimpleName());
	}

	@Override
	public void onActivityStarted(Activity activity) {
		String name = activity.getClass().getSimpleName();
		CLog.i(TAG, "Activity onStarted: " + name + ", count=" + count);
		if (count == 0 || (Global.LOCKED && !"LoginActivity".equals(name))) {
			if (!Global.isThirdPartyPageOpened() && YJKD.login()){
				if(YJKD.getInstance().isFingerprintOpen() && YJKD.getInstance().hasFingerprint()) {
					if (!"DisplayLeakActivity".equals(name)) {
						Global.LOCKED = true;
						FingerprintValidateActivity.start(activity);
					}
				}else if(GesturesPresenter.readStatus() && YJKD.getInstance().hadLockPattern()) {
					if (!"DisplayLeakActivity".equals(name)){
						Global.LOCKED = true;
						ValidateGesturesActivity.start(activity);
					}
				}
			}
		}
		count++;
		Global.BACKGROUND = false;
	}

	@Override
	public void onActivityResumed(Activity activity) {
		CLog.i(TAG, "Activity onResumed: " + activity.getClass().getSimpleName());
	}

	@Override
	public void onActivityPaused(Activity activity) {
		CLog.i(TAG, "Activity onPaused: " + activity.getClass().getSimpleName());
	}

	@Override
	public void onActivityStopped(Activity activity) {
		CLog.i(TAG, "Activity onStopped: " + activity.getClass().getSimpleName());
		count--;
		if(count == 0){
			Global.BACKGROUND = true;
		}
	}

	@Override
	public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
		CLog.i(TAG, "Activity onSaveInstanceState: " + activity.getClass().getSimpleName());
	}

	@Override
	public void onActivityDestroyed(Activity activity) {
		CLog.i(TAG, "Activity onDestroyed: " + activity.getClass().getSimpleName());
	}
}